package com.itheima.health.h5.controller;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.WxPayService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WxPayController {

    @Reference
    private WxPayService wxPayService;

    @Reference
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("wxPay/payNotify")
    public void wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 1.接收请求参数（xml）
            ServletInputStream in = request.getInputStream();
            // 2.将xml转为java对象
            XmlMapper xmlMapper = new XmlMapper();
            Map param = xmlMapper.readValue(in, Map.class);
            System.out.println("param = " + param);
            // 3.调用orderService，修改订单状态
            Boolean state = orderService.updateState(param);
            // 4.返回微信平台，接收成功..
            Map resultMap = new HashMap<>();
            resultMap.put("return_code", "SUCCESS");
            resultMap.put("return_msg", "OK");
            // 将map转为xml 通知微信收到
            String xml = xmlMapper.writeValueAsString(resultMap);
            response.setContentType("application/xml;charset=utf-8");
            response.getWriter().write(xml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("wxPay/createPayOrder/{orderId}")
    public Result createOrder(@PathVariable("orderId") Integer orderId) throws Exception {
        Map map = wxPayService.createOrder(orderId);
        return new Result(map);
    }

    @GetMapping("wxPay/isPay/{orderNo}")
    public Result isPay(@PathVariable("orderNo") Integer orderNo) {
        Integer pay = orderService.isPay(orderNo);
        if (pay==1){
          String  telephone= orderService.findTelephone(orderNo);
            // mq  发送发短信内容 消息形式 投递 mq  队列上
            Map map = new HashMap();
            map.put("telephone",telephone);
            map.put("submittime",orderNo);
//        rabbitTemplate.convertAndSend("sms-success-exchange","",map);
        }
        return new Result(pay);
    }

    @GetMapping("order/deleteOrder/{oid}")
    public Result deleteOrder(@PathVariable("oid") Integer id) {
        return new Result(orderService.deleteOrder(id));
    }
}
